/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import etcore.webapp.grouptalk.common.dao.GroupDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.object.Group;

/**
 * GroupService implements queries and transactions on Group
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class GroupService extends BaseService {

	@Override
	public void deleteSingle(Object persistentInstance) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			GroupDAO dao = new GroupDAO();
			dao.deleteSingle(persistentInstance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		} finally {
			if (session != null)
				session.close();
		}
	}

	public Group findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			Group result = dao.findById(id);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findBy3Properties(String propertyName, Object value,
			String propertyName2, Object value2, String propertyName3,
			Object value3, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao
					.findBy3Properties(propertyName, value, propertyName2,
							value2, propertyName3, value3, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeBy3Properties(String propertyName, Object value,
			String propertyName2, Object value2, String propertyName3,
			Object value3) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findSizeBy3Properties(propertyName, value,
					propertyName2, value2, propertyName3, value3);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public Group findByName(String name) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			Group result = dao.findByName(name);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findSizeByProperty(propertyName, value);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeByNDid(Integer kid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findSizeByNDid(kid);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeByDid(Integer kid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findSizeByDid(kid);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findGroupsByNDid(Integer kid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findGroupsByNDid(kid, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findGroupsByDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findGroupsByDid(did);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findEnglishGroupsByDid(Integer did) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findEnglishGroupsByDid(did);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findLocalGroupsByDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findLocalGroupsByDid(did);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findGroupsByDid(Integer kid, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findGroupsByDid(kid, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public Group findGroupByGmid(Integer gmid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			Group result = dao.findGroupByGmid(gmid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeBy2Properties(String propertyName, Object value,
			String propertyName2, Object value2) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findSizeBy2Properties(propertyName, value,
					propertyName2, value2);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findFollowingGroupsCount(String userName) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findFollowingGroupsCount(userName);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}
	}

	public long findFollowingGroupsCount(String userName, int open) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findFollowingGroupsCount(userName, open);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}
	}

	@SuppressWarnings("rawtypes")
	public List findActiveGroupIDs(Integer open, Integer nav, Integer days,
			boolean local, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List result = dao.findActiveGroupIDs(open, nav, days, local, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findFollowingGroups(String userName, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findFollowingGroups(userName, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findFollowingGroups(String userName, int open,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findFollowingGroups(userName, open, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findRelatedGroupSize(int gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findRelatedGroupSize(gid);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findRelatedGroups(int gid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findRelatedGroups(gid, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findRelatedGroups(int gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findRelatedGroups(gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findNotRelatedGroups(int gid) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findNotRelatedGroups(gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findNotRelatedGroups(int gid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findNotRelatedGroups(gid, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findNotRelatedGroupSize(int gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findNotRelatedGroupSize(gid);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findFavouriteGroups(String userName, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findFavouriteGroups(userName, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findFavouriteGroups(String userName, int open,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findFavouriteGroups(userName, open, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findFavouriteGroupsSize(String userName) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findFavouriteGroupsSize(userName);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findFavouriteGroupsSize(String userName, Integer open) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findFavouriteGroupsSize(userName, open);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findByProperty(propertyName, value, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findBy2Properties(String propertyName, Object value,
			String propertyName2, Object value2, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findBy2Properties(propertyName, value,
					propertyName2, value2, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findEnglishGroupsByNotInDirectories(Integer open,
			Integer nav, Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findEnglishGroupsByNotInDirectories(open,
					nav, parent);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findLocalGroupsByNotInDirectories(Integer open,
			Integer nav, Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findLocalGroupsByNotInDirectories(open,
					nav, parent);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findGroupsByNotInDirectories(Integer open, Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findGroupsByNotInDirectories(open, parent);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findEnglishGroupsByNotInDirectories(Integer open,
			Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findEnglishGroupsByNotInDirectories(open,
					parent);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findLocalGroupsByNotInDirectories(Integer open,
			Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findLocalGroupsByNotInDirectories(open,
					parent);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findGroupsByNotInDirectories(Integer open, Integer nav,
			Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findGroupsByNotInDirectories(open, nav,
					parent);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long searchSizeByProperty(String propertyName, String value) {

		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.searchSizeByProperty(propertyName, value);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> searchByProperty(String propertyName, String value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.searchByProperty(propertyName, value,
					begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findAllSize() {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.findAllSize();
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findAll(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Group> findAll() {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			List<Group> result = dao.findAll();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long isUsedName(String name) {
		Session session = HibernateSessionFactory.getSession();
		try {
			GroupDAO dao = new GroupDAO();
			long result = dao.isUsedName(name);
			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

}